package cn.xdf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lscl
 * @version 1.0
 * @intro: @PathVariable注解
 */
@Controller
@RequestMapping("/demo01")
public class Demo01Controller_PathVariable {

    //http://localhost:8080/demo01/10/zhangsan  地址栏传入id，和name，拿到数据写到前端
    @GetMapping("/{id}/{name}")
    //@RequestMapping(method = RequestMethod.GET)//接受get请求,没有写，getmapping也是包含了
    public void test(
            @PathVariable Integer id,
            @PathVariable(value = "name") String cityName,                  // 把url上的name值赋值给cityName
            HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("id: "+id+"<hr />");
        response.getWriter().write("cityName: "+cityName+"<hr />");
    }

    @GetMapping("/{id}")
    public void test2(
            @PathVariable Integer id,
            @PathVariable(value = "aaa",required = false) String cityName,// 没有aaa占位符也没关系,因为required是false,500
            HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("id: "+id+"<hr />");
        response.getWriter().write("cityName: "+cityName+"<hr />");
    }

}
